﻿$(function () {
	if (typeof ($.jqm) == "object") {
		prepareJqmodal();
	}
});


// TODO: nice to have: resize on iframe load
function prepareJqmodal() {
	// initialise jqModal
	window.modals = [];

	var handleJqWindow = function (hash, mode) {
		var $trigger = $(hash.t),
			$modal = $(hash.w)
				.width('')
				.height('')
				.css("top", '')
				.css("left", '');
		dialogStyle = $trigger.attr("data-dialogstyle"),
			dialogSpan = $trigger.attr("data-dialogspan"),
			onJqmShow = $trigger.attr("data-onshow"),
			myUrl = $trigger.attr("href");

		if ($modal.data("modaladded") != true) {
			$modal.data("dialogSpan", dialogSpan);
			window.modals.push($modal);
			$modal.data("modaladded", true);
		}

		$modal.addClass(dialogStyle);
		$("h3", $modal).text($trigger.attr("title") || "");

		$modal.css("left", (window_width - $modal.width()) / 2);
		if (dialogSpan == "fill") {
			$modal.css("top", window_height * 0.1 / 2)
					.height(window_height * 0.90);
		}

		if (mode == "iframe") {

			$modalContent = $("iframe", $modal);
			// cant hide content from firefox!
			$modalContent
				.bind("load", function () {
					//$modalContent.show();
					$modal.find(".loading").hide();
				})
				.html('')
				.attr("src", myUrl);

		}

		$modal.show();
		// reshape iframe
		if (mode == "iframe") {
			$modalContent.height($modal.height() - $("h3", $modal).outerHeight(true));
		}

	}

	var handleLoad = function (hash, mode) {
		// first rewire behaviors for ajaxed content
		if (mode == 'ajax') {
			rewireload($(hash.w));
		}
		//  add stub for onshow // this is probably too early?
		var onJqmShow = $(hash.t).attr("data-onshow");

		if (onJqmShow && typeof (eval(onJqmShow)) == "function") {
			eval(onJqmShow).call(hash);
		}

	};
	// prepare iframe dialogs
	var frameDialogs = $("#frameDialog");
	var closeIframeModal = function (hash) {
		// close then show loading, also remove src
		$modal = $(hash.w);
		var $modalContent = $("iframe", $modal).attr("src", '');

		hash.o.remove();
		$modal.hide().find(".loading").show();
	};

	frameDialogs.jqm({
		modal: true,
		overlay: 70,
		trigger: ".jqhandle",
		target: "#jqmContent",
		onShow: function (hash) {
			handleJqWindow(hash, 'iframe');
		},
		onHide: closeIframeModal
	});

	window.gframeDialogs = frameDialogs;

	// preparing ajax dialogs
	// delegate on mouse over of handlers

	$("#ajaxDialog").jqm({
		modal: false,
		overlay: 70,
		trigger: ".jqajax",
		target: "#jqmContent",
		ajax: '@href',
		ajaxText: '<div class="loading">loading...</div>',
		onShow: function (hash) {
			handleJqWindow(hash, 'ajax');
		},
		onLoad: function (hash) {
			handleLoad(hash, 'ajax');
		}
	});


	// preparing content dialogs
	if ($("#contentDialog").length > 0) {
		var newHtml = $("#contentDialog").html();

		// REDO jqcontent, jul 12, shouldve done that a long time ago
		$.each($(".dialogcontent"), function (i, o) {
			// get handler
			var o = $(o);
			var h = o.attr("data-jqhandle");

			// insert the close button and h3 using title attribute
			o.wrapInner('<div id="jqmContent"></div>');

			o.prepend(newHtml);

			o.jqm({
				modal: true,
				overlay: 70,
				trigger: h,
				onShow: function (hash) {
					handleJqWindow(hash, 'inpage');
					handleLoad(hash, 'inpage');
				}
			});
		});
	}
	$(window).resize(function () {
		// supposidly window_width and height are set
		// read window.modals one by one and change css values, doesnt matter if they are hidden

		$.each(window.modals, function (i, o) {

			o.css("left", (window_width - o.width()) / 2);
			if (o.data("dialogSpan") == "fill") {
				o.css("top", window_height * 0.1 / 2)
					.height(window_height * 0.90);
			}
		});

	});


}
function wireUpModals(context) {
	var c = (context) ? context.find("a.jqhandle") : "a.jqhandle";
	var q = (context) ? context.find("a.jqajax") : "a.jqajax";
	$("#frameDialog").jqmAddTrigger(c);
	$("#ajaxDialog").jqmAddTrigger(q);
}
function jqmParentHide() {
	// when triggered from an opened dialog, look for parent jqm object and close
	window.top.gframeDialogs.jqmHide();
}
function jqmDialogHide() {
	try {
		$('#ajaxDialog').jqmHide();
	} catch (e) {
		// when triggered from an opened dialog, look for parent jqm object and close
		window.top.gframeDialogs.jqmHide();
	}
}